<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>知识库添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <!-- 建议使用本地LayUI库替代CDN -->
    <link rel="stylesheet" href="/assets/common/libs/layui/css/layui.css" media="all">
</head>

<body>

    <div class="layui-container" style="margin-top: 30px;">
        <div class="layui-card">
            <div class="layui-card-header">添加知识库</div>
            <div class="layui-card-body">
                <form class="layui-form" id="knowledgeForm">
                    <div class="layui-form-item">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-block">
                            <input type="text" name="title" id="title" required lay-verify="required"
                                placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">内容</label>
                        <div class="layui-input-block" style="width: 100%;">
                            <textarea class="layui-textarea" id="content" style="display: none;"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="submitForm">提交</button>
                            <button type="button" class="layui-btn layui-btn-primary" id="cancelBtn">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- 建议使用本地LayUI库替代CDN -->
    <script src="/assets/common/libs/layui/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['layedit', 'form'], function () {
            var layedit = layui.layedit
                , form = layui.form
                , $ = layui.jquery;

            //构建富文本编辑器
            var contentEditor = layedit.build('content', {
                tool: ['strong', 'italic', 'underline', 'del', '|', 'fontFomatt', 'face', 'image', '|',
                    'left', 'center', 'right', '|', 'link', 'unlink', '|', 'code', 'preview']
                , height: 400
            });

            //表单提交 - 已取消注释并修正URL
            form.on('submit(submitForm)', function (data) {
                //获取标题
                var title = $('#title').val();
                //获取富文本内容
                var content = layedit.getContent(contentEditor);

                console.log('标题:', title);
                console.log('内容:', content);

                //显示加载状态
                var index = layer.load(1, { shade: [0.1, '#fff'] });

                //AJAX提交逻辑 - 已取消注释并修正URL为正确的后端接口
                $.ajax({
                    url: '/knowledgeBase/add', // 修改为正确的后端接口地址
                    type: 'POST',
                    data: {
                        title: title,
                        content: content
                    },
                    success: function (res) {
                        layer.close(index);
                        if (res.code === 0 || res.success === true) {
                            layer.msg('添加成功', function () {
                                //添加成功后跳转到列表页
                                window.location.href = '/knowledgeBase';
                            });
                        } else {
                            layer.msg('添加失败: ' + (res.msg || '未知错误'));
                        }
                    },
                    error: function () {
                        layer.close(index);
                        layer.msg('网络错误，请稍后重试');
                    }
                });

                return false; //阻止表单默认提交
            });

            //取消按钮
            $('#cancelBtn').click(function () {
                //可以清空表单或返回上一页
                layer.confirm('确定要取消添加吗？已填写的内容将丢失。', {
                    btn: [ '确定', '取消'] // 按钮
                }, function () {
                    // 返回列表页
                    window.location.href = '/knowledgeBase';
                }, function () {
                    // 取消操作，不做任何事情
                    layer.closeAll();
                });
            });
        });
    </script>

</body>

</html>